package cn.maihe.elg.operation.centers.fuzhou.handler.service;

import cn.hutool.core.codec.Base64;
import cn.maihe.elg.operation.centers.fuzhou.config.FZMakeInfoConfig;
import cn.maihe.elg.operation.centers.fuzhou.dto.handler.PayUrlReqDTO;
import cn.maihe.elg.operation.centers.fuzhou.dto.handler.PayUrlRespDTO;
import cn.maihe.elg.operation.centers.fuzhou.handler.FZCenterTradeService;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.AcceptOrderStatus;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.CenterProviderEnum;
import cn.maihe.elg.operation.model.enums.TradeType;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 出函机构平台作为服务方，电子保函管理平台作为请求方，向出函机构（保险公司/银行）发起支付请求，出函机构（保险公司/银行）返回支付的url地址
 * @Author WJH
 * @Date 2022/04/01
 */
@CenterTradeService(
        provider = CenterProviderEnum.ESB_FUZHOU,
        centerNo = CenterNoEnum.FUZHOU,
        tradeType = TradeType.payUrl,
        reqClass = PayUrlReqDTO.class,
        respClass = PayUrlRespDTO.class
)
@Slf4j
public class FZPayUrlService extends FZCenterTradeService<PayUrlReqDTO, PayUrlRespDTO> {

    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private ElgResourceService elgResourceService;

    public FZPayUrlService(FZMakeInfoConfig centerInfosConfig) {
        super(centerInfosConfig);
    }

    @Override
    public PayUrlRespDTO processService(CenterTradeContext<PayUrlReqDTO, PayUrlRespDTO> context) {
        PayUrlReqDTO req = context.getReq();
        String acceptOrderNo = req.getInsuranceNo();

        AcceptOrderInfoDO acceptOrderInfo = acceptOrderInfoService.getByAcceptOrderNo(acceptOrderNo);
        Assert.notNull(acceptOrderInfo, "查无此投保单号");

        if (AcceptOrderStatus.WAIT_SUBMIT_TUNE_UP.getStatus() != acceptOrderInfo.getStatus() &&
                AcceptOrderStatus.WAIT_PAY.getStatus() != acceptOrderInfo.getStatus()
        ) {
            throw new RuntimeException("投保单号非投保材料已推送状态,暂无法获取支付地址");
        }


        AcceptOrderInfoDO update = AcceptOrderInfoDO.builder().id(acceptOrderInfo.getId())
//                .status(AcceptOrderStatus.WAIT_PAY.getStatus()).remark("已获取支付地址待支付")
                .status(AcceptOrderStatus.WAIT_PAY_CONFIRM.getStatus()).remark("已获取支付地址待支付确认")
                .build();
        acceptOrderInfoService.updateById(update);

        PayUrlRespDTO respDTO = PayUrlRespDTO.success("支付地址获取成功", PayUrlRespDTO.class);
        PayUrlRespDTO.BizData bizData = new PayUrlRespDTO.BizData();
        bizData.setPayUrl(this.buildPayUrl(acceptOrderInfo));
        respDTO.setData(bizData);

        return respDTO;
    }

    /**
     * 获取该定的支付地址：无需授权地址
     *
     * @param acceptOrderInfo
     * @return
     */
    private String buildPayUrl(AcceptOrderInfoDO acceptOrderInfo) {
        Map<String, Object> params = new HashMap<>();
        params.put("bidCustomerId", acceptOrderInfo.getBidCustomerId());
        params.put("acceptOrderNo", acceptOrderInfo.getAcceptOrderNo());
        String paramsJson = JSON.toJSONString(params);

        String paramsB64 = Base64.encode(paramsJson);

        String payUrl = elgResourceService.buildElgFrontUrl("/#/payUrl/"+paramsB64);
        return payUrl;
    }

}
